//
//  WZRefuseViewController.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/8/31.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "WZRefuseViewController.h"
#import "JKAlertDialog.h"


#define TaskManager_Assigner_Authorized_Remind @"原因不能为空"
#define TaskManager_Assigner_Authorized_Reason @"拒绝原因说明"
#define TaskManager_Assigner_Authorized_Editing @""
#define TaskManager_AlertView_DoneButton @"确定"
#define TaskManager_AlertView_CancelButton @"取消"

@interface WZRefuseViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *refuseTextView;
@property (nonatomic, weak) IBOutlet UILabel *characterCount;

@end

@implementation WZRefuseViewController

- (void)awakeFromNib {
    [super awakeFromNib];
//    self.refuseTextView.layer.cornerRadius = 5;
//    self.refuseTextView.layer.masksToBounds = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setupRefuseView];
}


- (void)setupRefuseView {
    
    self.view.backgroundColor = RGBCOLORWITH16COLOR(0xf5f5f5);
    self.navigationItem.title = @"拒绝原因";
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"call_back" title:@"返回" target:self sel:@selector(backBtnDidClick) titleFont:Font18 titleColor:WhiteColor];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"taskManager_refuse_nav_right" title:nil target:self sel:@selector(refuseBtnDidClick) titleFont:Font12 titleColor:WhiteColor];
    
    self.refuseTextView.layer.cornerRadius = 5;
    self.refuseTextView.layer.masksToBounds = YES;
}

- (void)refuseBtnDidClick {

    if ([self.oaUserId isEqualToString:self.assignerId]) { // 分配人拒绝审核
        
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"name"] = self.oaUserName;
        params[@"pwd"] = self.oaPwd;
        params[@"exApplyId"] = self.taskId;
        params[@"result"] = @"2"; //1 接受 2 拒绝
        params[@"refuseReason"] = self.refuseTextView.text;
        
        [NetWorkShare assignerAuthorizedWithParams:params SuccessHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            if ([[responseObject objectForKey:@"success"] boolValue]) {
                
                [MBProgressHUD showError:@"您已拒绝!"];
                
                [self.navigationController popViewControllerAnimated:YES];
                
            } else {
                
                [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
            }
            
        } FailureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:NETWORKNERROR];
        }];
        
    } else if ([self.oaUserId isEqualToString:self.executorId]) { // 执行人拒绝任务
        
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"name"] = self.oaUserName;
        params[@"pwd"] = self.oaPwd;
        params[@"taskId"] = self.taskId;
        params[@"result"] = @"2"; //1 接受 2 拒绝
        params[@"refuseReason"] = self.refuseTextView.text;
        
        [NetWorkShare acceptAndRefuseWithParams:params SuccessHandle:^(AFHTTPRequestOperation *operation, id responseObject) {
            if (responseObject) {
                
                if ([responseObject objectForKey:@"success"]) {
                    WZLog(@"result = %@", responseObject);
                    
                    [MBProgressHUD showError:@"您已拒绝!"];
                    
                    [self.navigationController popViewControllerAnimated:YES];
                    
                } else {
                    
                    [MBProgressHUD showError:[responseObject objectForKey:@"message"]];
                }
            } else {
                
                [MBProgressHUD hideHUD];
                [MBProgressHUD showError:UNKNOWNERROR];
            }
        } FailureHandle:^(AFHTTPRequestOperation *operation, NSError *error) {
            [MBProgressHUD showError:NETWORKNERROR];
            WZLog(@"error = %@", error);
        }];
        
    } else {
        
        WZLog(@"其他拒绝情况!");
    }
}

#pragma mark - textView Delegate

- (void)textViewDidBeginEditing:(UITextView *)textView {
    
    WZLog(@"DidBeginEditing");
    
    if ([textView.text isEqualToString:TaskManager_Assigner_Authorized_Reason]) {
        
        textView.text = @"";
        [textView setTextColor:[UIColor blackColor]];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    
    WZLog(@"textViewDidEndEditing");
    
    if (textView.text.length < 1) {
        
        textView.text = TaskManager_Assigner_Authorized_Reason;
        [textView setTextColor:[UIColor grayColor]];
        textView.backgroundColor = WZRGBColor(247, 246, 250);
    }
}


- (void)textViewDidChange:(UITextView *)textView {
    
    WZLog(@"textViewDidChange");
    
    if (textView.text.length <= 500) {
        self.characterCount.text = [NSString stringWithFormat:@"%lu/500", 500 - textView.text.length];
        
    } else {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            textView.text = [textView.text substringToIndex:500];
            _characterCount.text = @"0/500";
            [textView endEditing:YES];
        });
    }
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if(range.location > 500){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"输入的自字符数不能超过500"
                                                       delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
        
    }else{
        
        return YES;
    }
}

- (NSUInteger) unicodeLengthOfString: (NSString *) text {
    NSUInteger asciiLength = 0;
    for (NSUInteger i = 0; i < text.length; i++) {
        unichar uc = [text characterAtIndex: i];
        asciiLength += isascii(uc) ? 1 : 2;
    }
    NSUInteger unicodeLength = asciiLength / 2;
    if(asciiLength % 2) {
        unicodeLength++;
    }
    return unicodeLength;
}


- (void)backBtnDidClick {
    
    [_refuseTextView endEditing:YES];
    
    if ([_refuseTextView.text isEqualToString:TaskManager_Assigner_Authorized_Reason]) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    
    __block WZRefuseViewController *block = self;
    JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:nil message:TaskManager_Assigner_Authorized_Reason];
    [alert addButton:Button_OK withTitle:TaskManager_AlertView_DoneButton handler:^(JKAlertDialogItem *item) {
        
        //        [block saveProgress];
        [block.navigationController popViewControllerAnimated:YES];
    }];
    
    [alert addButton:Button_CANCEL withTitle:TaskManager_AlertView_CancelButton handler:^(JKAlertDialogItem *item) {
        
        
    }];
    
    [alert show];
}


@end
